<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class OrderController extends Controller
{
    //订单列表
    public function index(Request $request)
    {
    	$title="订单列表";

        //会话跟踪
        $staff = $request->session()->get('staff');

    	//搜索查询数据
    	$data = DB::table('bro_order') ->where(function($query) use ($request){
            $query -> where('ordernum','like','%'.$request -> input('keywords').'%');
        }) -> paginate($request -> input('num',10));

        //向用户列表发送数据
        return view('admin.order.index',['title' => $title,'data' => $data,'request' => $request -> all(),'staff' => $staff]);

    }

    //订单编辑页面方法
    public function edit(Request $request,$id)
    {
        $title= '订单编辑';


        //会话跟踪
        $staff = $request->session()->get('staff');

        //执行数据查询
        $data = DB::table('bro_order') -> where('id',$id) -> first();
        //解析模板
        return view('/admin/order/edit',['title'=>$title,'data'=>$data,'staff' => $staff]);
    }

    //订单修改方法
    public function update(Request $request)
    {
    	//删除隐藏域
        $data = $request -> except('_token');
        // dd($data);

        //检测添加页面的数据并执行响应
       $this -> validate($request,[
            'utname' => 'required',
            'address' => 'required',
            'post' => 'required|max:6|min:6',
            'uphone' => 'required|regex:[1[0-9]{10}]|digits_between:11,11',
            'indent_price' => 'required',

        ],[

            'utname.required' => '联系人不能为空',
            'address.required' => '收货地址不能为空',
            'post.required' => '邮编不能为空',
            'post.max' => '请输入正确的邮编',
            'post.min' => '请输入正确的邮编',
            'uphone.required' => '手机号码不能为空',
            'uphone.regex' => '请输入正确的手机号码',
            'uphone.digits_between' => '请输入11位的有效手机号',
            'indent_price.required' => '订单金额不能为空',

        ]);

       // 发送修改语句
        $res = DB::table('bro_order') -> where('id',$data['id']) -> update($data);


        //判断修改是否成功
        if($res)
        {
            return redirect('/admin/order/index') -> with('success','更新成功');
        }else{
            return back() -> with('error','更新失败');
        }

    }

    //订单详情
    public function detail(Request $request,$id)
    {
    	 $title= '订单详情';

        //会话跟踪
        $staff = $request->session()->get('staff');

        //执行数据查询
        $data = DB::table('bro_indent') -> where('ordernum',$id) -> get();
        //解析模板
        return view('/admin/order/detail',['title'=>$title,'data'=>$data,'staff'=>$staff]);
    }

    //删除订单详情的商品资料
    public function delete($id)
    {   

        $data = DB::table('bro_indent')-> where('id',$id) -> first();
        
        // dd($data);
        $pic = $data -> goods_pic;

        $oldpath = '.'.$pic;

        if($oldpath != '.')      
        {
        	if(file_exists($oldpath))
        	{
            
              unlink($oldpath);
            
            
        	}
        }
        // dd($pic);
        $res = DB::table('bro_indent')-> where('id',$id) -> delete();
        $ordernum = $data -> ordernum;

        //判断删除是否成功
       if($res)
        {
            return redirect('/admin/order/detail/'.$ordernum) -> with('success','删除成功');
        }else{
            return back() -> with('error','删除失败');
        }
    }

    //删除订单
    public function del($id)
    {
    	//判断订单中是否有商品详情
    	$res = DB::table('bro_indent') -> where('indent_id',$id) ->get();
    	// dd($res);
    	//判断
    	if(!empty($res))
    	{
    		return back() -> with('error','对不起!该订单中有商品详情!');
    	}

    	//删除订单
    	$res = DB::table('bro_order') -> where('id',$id) -> delete();

    	//判断
    	if($res)
    	{
            return redirect('/admin/order/index') -> with('success','删除成功');
        }else{
            return back() -> with('error','删除失败');
        }	
    }

    //商品发货
    public function status(Request $request)
    {
    	//修改商品的状态
    	
    	$data['status'] = 1;

    	$res = DB::table('bro_indent') -> where('id',$request -> id) -> update($data);
    	// dd($res);

    	//判断
    	 return response() -> json($res);
    }
}
